package com.android.lovingpet.home.view;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.util.Log;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.android.lovingpet.R;
import com.android.lovingpet.bean.Body;
import com.android.lovingpet.common.OnItemClickListener;
import com.bumptech.glide.Glide;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by ikata on 2018/6/13.
 */

public class AdoptionRvAdapter extends RecyclerView.Adapter<AdoptionRvAdapter.MyViewHolder> implements View.OnClickListener {
    private Context context;
    private List<Body> list;
    private OnItemClickListener onItemClickListener;
    @Override
    public MyViewHolder onCreateViewHolder(ViewGroup parent, int viewType) {
        context=parent.getContext();
        View view= LayoutInflater.from(context).inflate(R.layout.rv_pet_or_host,parent,false);
        view.setOnClickListener(this);
        return new MyViewHolder(view);
    }
    public AdoptionRvAdapter()
    {
       list=new ArrayList<>();
    }

    public Body getData(int position)
    {
     return list.get(position);
    }
    public void add(Body body)
    {
        list.add(body);
        notifyItemInserted(list.size()-1);
    }

    public void clear()
    {
        list.clear();
        notifyDataSetChanged();
    }
    public void setOnItemClickListener(OnItemClickListener onItemClickListener)
    {
        this.onItemClickListener=onItemClickListener;
    }

    @Override
    public void onBindViewHolder(MyViewHolder holder, int position) {
        holder.itemView.setTag(position);
        Glide.with(context)
                    .load(list.get(position).getImage())
                    .error(R.drawable.defaluthead)
                    .into(holder.ivHead);
        holder.tvDescription.setText(list.get(position).getDescription());
        holder.ivIcon.setImageResource(R.drawable.icon_address);
        holder.tvAddress.setText(list.get(position).getAddress());

    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    @Override
    public void onClick(View v) {
        onItemClickListener.onItemClick((int)v.getTag());
    }

    public class MyViewHolder extends RecyclerView.ViewHolder{

        private TextView tvDescription;
        private ImageView ivHead;
        private ImageView ivIcon;
        private TextView tvAddress;

        public MyViewHolder(View itemView) {
            super(itemView);
            tvAddress=itemView.findViewById(R.id.tv_address);
            tvDescription=itemView.findViewById(R.id.tv_description);
            ivHead=itemView.findViewById(R.id.iv_head);
            ivIcon=itemView.findViewById(R.id.iv_icon);
        }
    }
}
